#!/bin/bash

if [ $# -lt 2 ]; then
  echo "usage: ren srcfile destname"
  exit 1
fi
params=($@)
destname=${params[$[$#-1]]}
if [ -e $destname ]; then
  echo "error: destname is filename!"
  exit 2
fi
for((i=0; i<$[$#-1]; i=$[$i+1])); do
  param=${params[$i]}
  for((j=$[${#param}-1]; j>0; j=$[$j-1])); do
    if [ ${param:$j:1} = . ]; then
      break
    fi
  done
  if ! [ $j -eq 0 ]; then
    extname=${param:$j}
  else
    extname=''
  fi
  if [ -e $param ] && ! [ -d $param ]; then
    mv $param $destname$i$extname
  fi
done
